package io.gitee.icefairy.swclient4flink;

import org.apache.flink.core.fs.FileStatus;
import org.apache.flink.core.fs.Path;
import seaweedfs.client.FilerProto;

public class SwFileStatus implements FileStatus {
    private FilerProto.Entry fs;

    public SwFileStatus() {
    }

    public SwFileStatus(FilerProto.Entry fs) {
        this.fs = fs;
    }

    @Override
    public long getLen() {
        return fs.getAttributes().getFileSize();
    }

    @Override
    public long getBlockSize() {
        Long tot = 0L;
        for (FilerProto.FileChunk fileChunk : fs.getChunksList()) {
            tot = tot + fileChunk.getSize();
        }
        return tot;
    }

    @Override
    public short getReplication() {
        String rep=fs.getAttributes().getReplication();
        int nRep=0;
        for (int i = 0; i < rep.length(); i++) {
            int ii=Integer.parseInt(rep.substring(i,i+1));
            nRep=nRep+ii;
        }
        return Short.parseShort(""+nRep);
    }

    @Override
    public long getModificationTime() {
        return fs.getAttributes().getMtime();
    }

    @Override
    public long getAccessTime() {
        return fs.getAttributes().getMtime();
    }

    @Override
    public boolean isDir() {
        return fs.getIsDirectory();
    }

    @Override
    public Path getPath() {
        return new Path(fs.getName());
    }
}
